import { extendClassProps, PartialWithUndefinedReactivePropsToNativeProps, ReactivePropsToNativePropsAndChanged, reactJson, SceneObjectKey } from "xbsj-base";
import { ES3DTileset } from "../ES3DTileset";
import { ESRtsTileset } from "../ESRtsTileset";
import { SceneObjectPickedInfo } from "@sdkSrc/ESJTypes";
import { ESSceneObject } from "@sdkSrc/ESObjects/base";

function isNumber(value: any): boolean {
    return typeof value === 'number' && !isNaN(value);
}
export class ESRtsFeatureEditing extends ES3DTileset {
    static override readonly type = this.register('ESRtsFeatureEditing', this, { chsName: 'ESRtsFeatureEditing', tags: ['ESObjects', '_ES_Impl_Cesium', '_ES_Impl_UE'], description: "实时3DTileset图层" });
    // 重写get typeName方法，返回'ESRtsFeatureEditing'
    // 重写get typeName方法，返回'ESRtsFeatureEditing'
    override get typeName() { return 'ESRtsFeatureEditing'; }
    override get defaultProps() { return ESRtsFeatureEditing.createDefaultProps(); }
    override get json() {
        return {
            // @ts-ignore
            ...this._innerGetJson(),
            "show": this.show,
            "materials": this.materials,
            "beautifyMaterails": this.beautifyMaterails,
            "props": this.props,
            "rotation": this.rotation,
        } as JsonType;
    }
    override set json(value: JsonType) { this._innerSetJson(value); }

    public override combinationClass: boolean = true;
    public mainClass: undefined | ESSceneObject = undefined;
    public override getMainClass(): ESSceneObject { return this.mainClass ?? this }
    async init(eSRtsTileset: ESRtsTileset, featureId: string) {
        this.featureId = featureId;
        const uiShowprops = await eSRtsTileset.getFeatureProperty(featureId) as any;
        this.uiShowprops = uiShowprops.allFields.map((e: any) => {
            return {
                featureClassName: e.featureClassName,
                fields: e.fields.map((f: any) => {
                    return {
                        key: f.name,
                        type: f.type,
                        value: f.type != "Double" ? f.value : (isNumber(f.value) ? Number(f.value.toFixed(3)) : "")
                    }
                })
            }
        })
        this.datasetName = uiShowprops.datasetName

        //隐藏原来大的中的feature
        eSRtsTileset.setFeatureVisable('id', [{ value: this.featureId, visable: false }]);
        this.d(() => { eSRtsTileset.setFeatureVisable('id', [{ value: this.featureId, visable: true }]) });

        const ip = eSRtsTileset.tileServiceIp;//http://localhost
        const port = eSRtsTileset.tilesetServePort;//9014
        if (!port) {
            throw new Error('ESRtsFeatureEditing: port is not defined');
        }
        this.url = `${ip}:${port}/featuretiles/${this.featureId}/tileset.json`;
        this.allowPicking = true;

        //将ES3DTileset的pickedEvent事件绑定到ESDSFeature的pickedEvent事件上
        this.d(this.pickedEvent.don((pickedInfo) => {
            // @ts-ignore
            eSRtsTileset.pickedEvent.emit(new SceneObjectPickedInfo(eSRtsTileset, pickedInfo));
        }))

        //编辑互斥
        // {
        //     this.d(this.rotationEditingChanged.don((value) => {
        //         value && (this.editing = false);
        //     }))
        //     this.d(this.editingChanged.don((value) => {
        //         value && (this.rotationEditing = false);
        //     }))
        // }

        this.d(() => {
            this.editing = false;
            // this.rotationEditing = false
            eSRtsTileset.removeHighlightDSFeature(this)
        })
    }

    constructor(id?: SceneObjectKey) {
        super(id);


    }

    static override defaults = {
        ...ES3DTileset.defaults,
        layerConfig: {} as JsonType
    }

    override getProperties(language?: string) {
        return [
            ...super.getProperties(language),

        ]
    }
}



export namespace ESRtsFeatureEditing {
    export const createDefaultProps = () => ({
        ...ES3DTileset.createDefaultProps(),
        datasetName: undefined as string | undefined,
        featureId: undefined as string | undefined,
        materials: reactJson<any>({}),
        beautifyMaterails: reactJson<any>({}),
        uiShowprops: reactJson<any>({}),
        props: reactJson<any>({}),
    });
}
extendClassProps(ESRtsFeatureEditing.prototype, ESRtsFeatureEditing.createDefaultProps);
export interface ESRtsFeatureEditing extends ReactivePropsToNativePropsAndChanged<ReturnType<typeof ESRtsFeatureEditing.createDefaultProps>> { }
type JsonType = PartialWithUndefinedReactivePropsToNativeProps<ReturnType<typeof ESRtsFeatureEditing.createDefaultProps> & { type: string }>;





